package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import bean.Order;
import bean.ConnectMySql;

/**
 * Servlet implementation class submitCart
 */
@WebServlet("/submitCart")
public class submitCart extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public submitCart() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		ResultSet rs = null;
		PreparedStatement st1=null;
		Connection con=ConnectMySql.getConnection();
		ArrayList <Order> allOrder = new ArrayList<Order>();
//		try {
//			  HttpSession session=request.getSession();
//			  String userId=(String)session.getAttribute("xxx");
//		      st1=con.prepareStatement("select * from cart where userId=?");
//		      PrintWriter out=response.getWriter();
//		      st1.setInt(1,Integer.valueOf(userId));
//		      rs=st1.executeQuery();
//		      while(rs.next()){
//		    	    Order or = new Order();
//					or.setOrderId(rs.getInt(1));
//					or.setUserId(rs.getInt(2));
//					or.setDishId(rs.getInt(3));
//					or.setQuantity(rs.getInt(4));
//					out.print(rs.getInt(3));
//					allOrder.add(or);
//		    	  PreparedStatement st=null;
//		    	  st=con.prepareStatement("insert into order values(null,?,?,?)");
//		          st.setInt(1,rs.getInt(2));
//			      st.setInt(2,rs.getInt(3));
//			      st.setInt(3,rs.getInt(4));
//			      st.executeUpdate();
//		          ConnectMySql.closeStatement(st);
//			      out.print(rs.getInt(2)+" "+rs.getInt(3)+" "+rs.getInt(4));
//		      }

//		    	  st1=con.prepareStatement("delete from cart where userId=?");
//			      st1.setInt(1,Integer.valueOf(userId));
//			      st1.executeUpdate();

//		      for(Order good:allOrder){	
//		    	  st=con.prepareStatement("insert into order values(null,?,?,?)");
//		          st.setInt(1,Integer.valueOf(userId));
//			      st.setInt(2,good.getDishId());
//			      st.setInt(3,good.getQuantity());
//			      //out.print(good.getDishId());
//			      st.executeQuery();
//			      out.print(good.getDishId());
//		      }
//		      ConnectMySql.closeResultSet(rs);
//	          ConnectMySql.closeStatement(st1);
//	  		  ConnectMySql.closeConnection(con);
//			}catch(SQLException e) {
//				e.printStackTrace();
//		}
		response.sendRedirect("submitPage.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
